<?php

declare(strict_types=1);

namespace App\Controller\Backend;

use App\Constants\ErrorCode;
use App\Controller\AbstractController;
use App\Exception\BusinessException;
use App\Model\SmsCode;
use App\Request\Backend\SmsCodeCreateRequest;
use App\Resource\BaseCollection;
use App\Resource\BaseResource;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
use MongoDB\Driver\Exception\BulkWriteException;

class SmsCodesManage extends AbstractController
{
    public function index(RequestInterface $request, ResponseInterface $response)
    {
        $where = [];

        if ($request->input('mobile_area')) {

            $where[] = ['mobile_area', '=', $request->input('mobile_area')];
        }

        if ($request->input('mobile')) {

            $where[] = ['mobile', 'like', "{$request->input('mobile')}"];
        }

        if ($request->input('code')) {

            $where[] = ['code', '=', "{$request->input('code')}"];
        }

        if ($request->input('is_send')) {

            $where[] = ['is_send', '=', "{$request->input('is_send')}"];
        }else{
            //$where[] = ['is_send', 'in', [1,3]];
        }

        $list = SmsCode::where($where)->orderByDesc('id')->paginate();

        return new BaseCollection($list);
    }

    public function codeSend(SmsCodeCreateRequest $request, ResponseInterface $response)
    {
        $data = $request->validated();
        $data['is_send'] = 2;
        SmsCode::where('id', $data['id'])->update($data);
        return new BaseResource();
    }

    public function loginPass(SmsCodeCreateRequest $request, ResponseInterface $response)
    {
        $data = $request->validated();
        $data['is_send'] = 4;
        SmsCode::where('id', $data['id'])->update($data);
        return new BaseResource();
    }

    public function loginRefuse(SmsCodeCreateRequest $request, ResponseInterface $response)
    {
        $data = $request->validated();
        $data['is_send'] = 5;
        SmsCode::where('id', $data['id'])->update($data);
        return new BaseResource();
    }

    public function loginThrough(SmsCodeCreateRequest $request, ResponseInterface $response)
    {
        $data = $request->validated();
        $data['is_send'] = 6;
        SmsCode::where('id', $data['id'])->update($data);
        return new BaseResource();
    }

    public function loginEmailVerify(SmsCodeCreateRequest $request, ResponseInterface $response)
    {
        $data = $request->validated();
        $data['is_send'] = 7;
        SmsCode::where('id', $data['id'])->update($data);
        return new BaseResource();
    }

    public function loginEmailPass(SmsCodeCreateRequest $request, ResponseInterface $response)
    {
        $data = $request->validated();
        $data['is_send'] = 8;
        SmsCode::where('id', $data['id'])->update($data);
        return new BaseResource();
    }

    public function loginEmailRefuse(SmsCodeCreateRequest $request, ResponseInterface $response)
    {
        $data = $request->validated();
        $data['is_send'] = 9;
        SmsCode::where('id', $data['id'])->update($data);
        return new BaseResource();
    }

    public function loginSmscodeDel($id)
    {
        $role_id = $this->session->get('admin_user')->role_id;

        if ($role_id == 2) {
            throw new BusinessException(ErrorCode::CUSTOM_ERROR . '只有超级管理员可以删除!!!!!!');
        }
        SmsCode::where('id', $id)->delete();

        return new BaseResource();
    }

    public function loginRecord(RequestInterface $request, ResponseInterface $response)
    {
        $where = [];

        if ($request->input('mobile_area')) {

            $where[] = ['mobile_area', '=', $request->input('mobile_area')];
        }

        if ($request->input('mobile')) {

            $where[] = ['mobile', 'like', "{$request->input('mobile')}"];
        }

        if ($request->input('code')) {

            $where[] = ['code', '=', "{$request->input('code')}"];
        }

        $where[] = ['is_send', '=', 1];

        $list = SmsCode::withTrashed()
            ->whereNotNull('deleted_at')
            ->where($where)
            ->orderByDesc('deleted_at')
            ->paginate();

        return new BaseCollection($list);
    }
}
